package demo9;


import java.util.HashMap;
import java.util.Map;

public class BinarySortTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int val) {
            this.val = val;
        }
    }


    public TreeNode root;

    public void insert(int val) {
        if(root == null) {
            root = new TreeNode(val);
            return;
        }
        TreeNode node = new TreeNode(val);
        TreeNode cur = root;
        TreeNode parent = null;
        while(cur != null) {
            parent = cur;
            if (cur.val > node.val) {
                //进到左边
                cur = cur.left;
            } else if (cur.val < node.val) {
                //进到右边
                cur = cur.right;
            } else {
                return;
            }
        }
        if(parent.val > node.val) {
            parent.left = node;
        }
        if(parent.val < node.val) {
            parent.right = node;
        }
    }

    /**
     * 搜索
     * 时间复杂度：
     * 最好情况：O(logN)  满二叉树
     * 最坏情况：O(N)     单只树
     * @param val
     * @return
     */
    public TreeNode search(int val) {
        if(root == null) {
            return null;
        }
        TreeNode cur = root;
        while(cur != null) {
            if (cur.val > val) {
                //进到左边
                cur = cur.left;
            } else if (cur.val < val) {
                //进到右边
                cur = cur.right;
            } else {
                return cur;
            }
        }
        return null;
    }

    /**
     * 删除
     * @param val
     */
    public void delete(int val) {
        if(root == null) {
            return;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        while(cur != null) {
            cur = parent;
            if (cur.val > val) {
                //进到左边
                cur = cur.left;
            } else if (cur.val < val) {
                //进到右边
                cur = cur.right;
            } else {
                removeNode(parent,cur);
            }
        }
    }

    private void removeNode(TreeNode parent, TreeNode cur) {
        if(cur.left == null) {
            if(cur == root) {
                cur = cur.right;
            }else if(parent.right == cur) {
                parent.right = cur.right;
            }else if(parent.left == cur) {
                parent.left = cur.right;
            }
        }else if(cur.right == null) {
            if(cur == root) {
                cur = cur.left;
            }else if(parent.right == cur) {
                parent.right = cur.left;
            }else if(parent.left == cur) {
                parent.left = cur.left;
            }
        }else {
            TreeNode tmpParent = cur;
            TreeNode tmp = cur.right;
            while(tmp.left != null) {
                tmpParent = tmp;
                tmp = tmp.left;
            }
            cur.val = tmp.val;
            if(tmpParent.left == tmp) {
                tmpParent.left = tmp.right;
            }
            if(tmpParent.right == tmp) {
                tmpParent.right = tmp.right;
            }
        }

    }
}
